package com.zretc.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.zretc.dao.VipMapper;
import com.zretc.dao.VipOrdersMapper;
import com.zretc.dao.VipTypeMapper;
import com.zretc.entity.Vip;
import com.zretc.entity.VipOrders;
import com.zretc.entity.VipType;
import com.zretc.service.BackVipOrdersService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;


import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


@Service
public class BackVipOrdersServiceImpl implements BackVipOrdersService {



    @Autowired
    VipOrdersMapper vipOrdersMapper;

    @Autowired
    VipTypeMapper vipTypeMapper;

    @Autowired
    VipMapper vipMapper;

    @Override
    public int insertVipOrdersInfo(VipOrders vipOrders) {


        //设置会员卡订单号
        if(vipOrders.getVipOrdersId() == null) {
            // 使用时间戳+随机数自动生成文件名,避免VipId重复问题
            // JDK1.8 日期时间类
            //获取当前日期
            LocalDateTime now = LocalDateTime.now();
            DateTimeFormatter ofPattern = DateTimeFormatter.ofPattern("yyyyMMddhhmmssSSS");
            // 将当前日期时间转成字符串
            String formatDate = ofPattern.format(now);
            // 随机数
            int random = (int) (Math.random() * 1000 + 1);
            // 拼接
            StringBuffer VipOrdersdId = new StringBuffer();
            VipOrdersdId.append(formatDate).append(random);
            //StringBuffer转String
            String newVipOrdersId = VipOrdersdId.toString();
            vipOrders.setVipOrdersId(newVipOrdersId);
        }

        //设置充值时间
        if(vipOrders.getVipStartTime() == null){
            //获取当前时间
            LocalDateTime now = LocalDateTime.now();
            DateTimeFormatter ofPattern = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
            // 将当前日期时间转成字符串
            String formatDate = ofPattern.format(now);


            vipOrders.setVipStartTime(formatDate);
        }

        //设置到期时间
        if(vipOrders.getVipEndTime() == null) {
                LocalDateTime now = LocalDateTime.now();


            //判断会员类型,设置不同有效期
            if ("M".equals(vipOrders.getVipTypeId())) {

                //月卡有效期
                now = now.plusDays(30);


            } else if ("S".equals(vipOrders.getVipTypeId())) {

                //季卡有效期
                now = now.plusMonths(3);

            } else if ("Y".equals(vipOrders.getVipTypeId())) {

                //年卡有效期
                now = now.plusYears(1);

            } else {

                //默认设置为月卡
                now = now.plusDays(30);

            }
            DateTimeFormatter nowDate = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");
            String nd = nowDate.format(now);
            vipOrders.setVipEndTime(nd);
        }

        //填充会员表
        Vip vip = new Vip();
        vip.setAccountId(vipOrders.getAccountId());
        vip.setVipTypeId(vipOrders.getVipTypeId());
        vip.setVipStartTime(vipOrders.getVipStartTime());
        vip.setVipRegisterTime(vipOrders.getVipStartTime());
        vip.setVipEndTime(vipOrders.getVipEndTime());
        vipMapper.insertVipInfo(vip);

        return vipOrdersMapper.insertVipOrdersInfo(vipOrders);
    }

    @Override
    public PageInfo<VipOrders>getVipOrdersInfoByVipOrdersId(String VipOrdersId,String page,String limit) {
                String page1 = page;
                if(page1 == null){
                   page1="1";
                }
                String limit1 = limit;
                if(limit1 == null){
                    limit1 ="10";
                }

                Integer pageNum = Integer.valueOf(page1);
                Integer pageSize = Integer.valueOf(limit1);
                PageHelper.startPage(pageNum,pageSize);
                VipOrders vipOrders = vipOrdersMapper.getVipOrdersInfoByVipOrdersId(VipOrdersId);
                   List<VipOrders> list = new ArrayList<>();
                   list .add(vipOrders);
                PageInfo<VipOrders> vioOdersPage = new PageInfo<>(list);

        return vioOdersPage ;
    }

    @Override
    public PageInfo<VipOrders> getAllVipOrdersInfo(String page, String limit) {

        String page1 = page;
        if(page1 == null){
            page1="1";
        }
        String limit1 = limit;
        if(limit1 == null){
            limit1 ="10";
        }

        Integer pageNum = Integer.valueOf(page1);
        Integer pageSize = Integer.valueOf(limit1);
        PageHelper.startPage(pageNum,pageSize);
        List<VipOrders> list = new ArrayList<>(vipOrdersMapper.getAllInfo());
        PageInfo<VipOrders> vipOrdersPageInfo = new PageInfo<>(list);
        return vipOrdersPageInfo;
    }

}
